package com.example.computer_exam_training_sys.mapper;

import com.example.computer_exam_training_sys.entity.User_Info;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface User_Info_Mapper {
    @Select("SELECT * from users")
    List<User_Info> selectAll();//遍历查询
    @Select("select * from users where user_id=#{user_id}")
    User_Info User_InfoselectByID(int user_id);//条件查询
    // 新增用户
    @Insert("INSERT INTO users (username, password, email, phone, real_name, gender, birth_date, register_time, last_login, status, avatar) VALUES (#{username}, #{password}, #{email}, #{phone}, #{realName}, #{gender}, #{birthDate}, now(), #{lastLogin}, #{status}, #{avatar})")
    int insertUser(User_Info user);
    // 更新用户
    @Update("UPDATE users SET username=#{username}, password=#{password}, email=#{email}, phone=#{phone}, real_name=#{realName}, gender=#{gender}, birth_date=#{birthDate}, register_time=#{registerTime}, last_login=#{lastLogin}, status=#{status}, avatar=#{avatar} WHERE user_id=#{userId}")
    int updateUser(User_Info user);
    // 删除用户
    @Delete("DELETE FROM users WHERE user_id=#{userId}")
    int deleteUser(int userId);

    @Select("SELECT * FROM users WHERE DATE(register_time) = CURDATE()")
    List<User_Info> getNewUsersToday();

    @Update("UPDATE users SET last_login = NOW() WHERE user_id = #{userId}")
    int updateLastLogin(@Param("userId") int userId);
    // ✅ 新增：用于登录验证
    @Select("SELECT * FROM users WHERE username = #{username}")
    User_Info findByUsername(@Param("username") String username);

    // 修改用户密码（只有原密码正确才更新）
    @Update("UPDATE users SET password = #{newPassword} WHERE user_id = #{userId} AND password = #{oldPassword}")
    int updatePassword(@Param("userId") int userId, @Param("oldPassword") String oldPassword, @Param("newPassword") String newPassword);

    // 更新用户头像
    @Update("UPDATE users SET avatar = #{avatar} WHERE user_id = #{userId}")
    int updateAvatar(@Param("userId") int userId, @Param("avatar") String avatar);

    @Select("SELECT COUNT(*) FROM users")
    int getTotalUserCount();
}

